import axios from "axios";
import router from './../router/index.js'
import { getSession } from "../utils/index.js";

import { Toast } from 'vant';
const http=axios.create({
    baseURL:"http://shop.bluej.cn/api",
    timeout:500
})
//axios拦截器 对返回的结果进行处理
http.interceptors.response.use(function (response) {
  
    return  Promise.resolve (response);
    
  }, function (error) {
    console.log(error);
   
    if (error.request.status==401) {
      Toast.clear()
       Toast.fail({ message: "请求失败，跳转至登陆", forbidClick: true });
       setTimeout(() => {
        router.push("/allLogin")
       }, 1000);
    }
    return Promise.reject(error.response)
  });
  http.interceptors.request.use(function (config) {

    let result=getSession("result")
    
     if (result) {
     
      config.headers['x-token']=result.token
     }
    
     return config

  }, function (error) {
    console.log(error);
   
    return error.response;
  });

export const post=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.post(url,data,{
            headers:{
                "Content-type": "application/json",
            }
        }
        ).then((res)=>{
          
            resolve(res);
        },err=>{
          reject(err)
        })
      
    })
}
export const get=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.get(url,data,{
          headers:{
              "Content-type": "application/json",
          }
      }

      ).then((res)=>{
         
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const deleteService=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.delete(url,{
          headers:{
              "Content-type": "application/json",
          },
          params:data
      }

      ).then((res)=>{
         
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const getData=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.get(url,{
            headers:{
                "Content-type": "application/json",
            },
            params:data
}
  
        ).then((res)=>{
           
            resolve(res);
        },err=>{
          reject(err)
        })
      
    })
  }
// 更改接口
export const put=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.put(url,data,{
          headers:{
              "Content-type": "application/json",
          }
      }

      ).then((res)=>{
          
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const otherget = (url, params = {}) => {
     return new Promise((resolve, reject) => {
    http.get(url, { params }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
  
  };
//上传用户头像
export const postFile = (url, data) => {
  return new Promise((resolve, reject) => {
      http.post(url, data, {
          headers: {
              'Content-Type': 'multipart/form-data'
          }
      })
          .then(res => {
              resolve(res);
          })
          .catch(err => {
              reject(err);
          });
  });
};
// 删除接口
export const deleteData =(url,data)=>{
  return new Promise((resolve, reject) => {
    http.delete(url, data, {
        headers: {
          "Content-type": "application/json",
        }
    })
        .then(res => {
            resolve(res);
        })
        .catch(err => {
            reject(err);
        });
});
}